define(function () {

    var callback;

    var params = {
        //获取页面上的参数
        get: function (args) {
            var str = '';
            args = args.split("&");

            for (var i = 0; i < args.length; i++) {
                str = args[i];
                var arg = str.split("=");
                if (arg.length <= 0) continue;
                if (arg.length == 1) params[arg[0]] = true;
                else params[arg[0]] = arg[1];
            }
        },
        //拼接为字符串
        toStr: function () {
            var str = '';
            for (var i in params) {
                if (params[i] !== null && i.length > 0 && typeof(params[i]) != 'function') {
                    if (str.length > 0) {
                        str += '&';
                    }
                    str += i + '=' + params[i];
                }
            }
            return str;
        },
        //置空,参数是保留列表
        clear: function (args) {
            var arr = args.join('#');

            for (var i in params) {
                if (params[i] !== null && i.length > 0 && typeof(params[i]) != 'function' && arr.match(i) == null) {
                    params[i] = null;
                }
            }
        }
    };

    var config = {
        isWX: navigator.userAgent.match(/MicroMessenger/i) != null,
        isMobile: navigator.userAgent.match(/mobile/i) != null,

        getCode:function(){
            params.get(window.location.search.substring(1));

            if (typeof(params.state) == 'undefined' && typeof(params.code) == 'undefined') {
                //1st
                var appId = 'wx744789961108d6b7';
                var REURI = encodeURI(location.origin + location.pathname);
                var state = enCodeUni('?' + params.toStr() + location.hash);
                location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?'
                    + 'appid=' + appId + '&redirect_uri=' + REURI + '&response_type=code&scope=snsapi_base&state=' + state + '#wechat_redirect';
            }
            else if (params.state && params.code) {
                //2nd
                var newurl = deCodeUni(params.state).split('#');
                params.get(newurl[0].substring(1) + '&code=' + params.code);
                params.clear(['listId', 'code']);
                params.showwxpaytitle = 1;
                location.href = location.origin + location.pathname + '?' + params.toStr() + '#' + newurl[1];
            }
            else if (params.showwxpaytitle == 1) {
                $.jsonp({
                    url: "http://api.meizhanggui.cc/WenShen/V2.0.0/Weixin/Public/accessToken",
                    callbackParameter: "callback",
                    data: {
                        code: params.code
                    },
                    async: false,
                    success: function (obj) {
                        obj = $.parseJSON(obj);
                        if (obj.code == 0) {
                            if (typeof(obj.data.errmsg) != 'undefined') {
                                if (obj.data.errmsg.match('invalid code')) {
                                    return config.getCode();
                                }
                            }
                            config.openId = obj.data.openid;
                            params.clear(['listId', 'showwxpaytitle']);
                            history.pushState(null, document.title, location.origin + location.pathname + '?' + params.toStr() + location.hash);
                            callback();
                        }
                    }
                });
            }
        },
        next:function(next){
            callback = next;
            //微信中获取code
            if (config.isWX&&(location.hash.match(/deal/i)||location.href.match(/deal\*5/i))) {
                config.getCode();
            }
            else {
                params.get(window.location.search.substring(1));
                params.clear(['listId', 'showwxpaytitle']);
                history.pushState(null, document.title, location.origin + location.pathname + '?' + params.toStr() + location.hash);
                callback();
            }
        }
    };

    //URL编码解码算法
    var enCodeUni = function (str) {
        return str.replace(/#/g, '*0').replace(/!/g, '*1').replace(/\?/g, '*2').replace(/=/g, '*3').replace(/&/g, '*4').replace(/\//g, '*5');
    };
    var deCodeUni = function (nstr) {
        return nstr.replace(/\*0/g, '#').replace(/\*1/g, '!').replace(/\*2/g, '?').replace(/\*3/g, '=').replace(/\*4/g, '&').replace(/\*5/g, '/');
    };

    return window.config = config;

});